package com.gdyx.app.web.svc.impl;

import com.gdyx.app.web.dao.*;
import com.gdyx.app.web.model.*;
import com.gdyx.app.web.svc.MpsUserSvc;
import com.gdyx.app.web.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Y_Coffee on 2017/8/3.
 */
@Service
public class MpsUserSvcImpl implements MpsUserSvc {
    @Autowired
    MpsUserDao dao;
    @Autowired
    MpsUserRoleDao mpsUserRoleDao;
    @Autowired
    MpsDeptUserDao mpsDeptUserDao;
    @Autowired
    MpsOrgUserDao mpsOrgUserDao;
    @Autowired
    MpsRoleDao mpsRoleDao;
    @Autowired
    MpsDeptDao mpsDeptDao;


    @Transactional
    @Override
    public void regSave(MpsUser o) {
        String uid = UUID.randomUUID().toString().toUpperCase().trim();
        o.setId(uid);
        o.setCreateOwner("By Register");
        o.setCreateTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
        o.setPasv(MD5Utils.string2MD5(o.getPasv()));
        dao.save(o);
    }

    @Transactional
    @Override
    public void save(MpsUser o, String Owner) {
        //用户增加
        String uid = UUID.randomUUID().toString().toUpperCase().trim();
        o.setId(uid);
        o.setCreateTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
        o.setCreateOwner(Owner);
        o.setPasv(MD5Utils.string2MD5(o.getPasv()));
        dao.save(o);
        //用户角色关联表增加
        if (o.getRoles() != null) {
            for (String rid : o.getRoles()) {
                MpsUserRole mpsUserRole = new MpsUserRole();
                mpsUserRole.setId(UUID.randomUUID().toString().toUpperCase().trim());
                mpsUserRole.setUserId(uid);
                mpsUserRole.setRoleId(rid);
                mpsUserRole.setCreateOwner(Owner);
                mpsUserRole.setCreateTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                mpsUserRoleDao.save(mpsUserRole);
            }
        }
        //用户部门关联表增加
        if (o.getDepts() != null) {
            for (String did : o.getDepts()) {
                MpsDeptUser mpsDeptUser = new MpsDeptUser();
                mpsDeptUser.setId(UUID.randomUUID().toString().trim().toUpperCase());
                mpsDeptUser.setUserId(uid);
                mpsDeptUser.setDeptId(did);
                mpsDeptUser.setCreateOwner(Owner);
                mpsDeptUser.setCreateTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                mpsDeptUserDao.save(mpsDeptUser);
            }
        }
        //用户机构关联表增加
        for (String oid : o.getOrgs()) {
            MpsOrgUser mpsOrgUser = new MpsOrgUser();
            mpsOrgUser.setId(UUID.randomUUID().toString().toUpperCase().trim());
            mpsOrgUser.setUserId(o.getId());
            mpsOrgUser.setOrgId(oid);
            mpsOrgUser.setOrgchk(0);
            mpsOrgUserDao.save(mpsOrgUser);
        }
    }

    @Override
    public void saveOrUpdate(MpsUser o) {
        dao.saveOrUpdate(o);
    }

    @Transactional
    @Override
    public void delete(String id,String orgId) {
        //重机构中剔除用户
        mpsOrgUserDao.deleteByOidAndUid(orgId, id);
        //删除用户角色关联表
        List<MpsRole> mpsRoleByOId = mpsRoleDao.findMpsRoleByOId(orgId);
        for (MpsRole mpsRole : mpsRoleByOId) {
            mpsUserRoleDao.deleteByUidAndRid(id,mpsRole.getId());
        }
        //删除用户部门关联表
        List<MpsDept> mpsDeptByOId = mpsDeptDao.findMpsDeptByOId(orgId);
        for (MpsDept mpsDept : mpsDeptByOId) {
            mpsDeptUserDao.deleteByUidAndDid(mpsDept.getId(),id);
        }
    }

    @Transactional
    @Override
    public void update(MpsUser o, String Owner) {
        //用户表更新
        o.setModifyTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
        o.setModifyOwner(Owner);
        o.setPasv(MD5Utils.string2MD5(o.getPasv()));
        dao.update(o);
        /**
         * 用户角色关联更新
         */
        //用户角色关联表删除
        mpsUserRoleDao.deleteByUid(o.getId());
        //用户角色关联表增加
        if (o.getRoles() != null) {
            for (String rid : o.getRoles()) {
                MpsUserRole mpsUserRole = new MpsUserRole();
                mpsUserRole.setId(UUID.randomUUID().toString().toUpperCase().trim());
                mpsUserRole.setUserId(o.getId());
                mpsUserRole.setRoleId(rid);
                mpsUserRole.setModifyOwner(Owner);
                mpsUserRole.setModifyTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                mpsUserRoleDao.save(mpsUserRole);
            }
        }
        /**
         * 用户部门关联更新
         */
        //用户部门关联表删除
        mpsDeptUserDao.deleteByUid(o.getId());
        //用户部门关联表增加
        if (o.getDepts() != null) {
            for (String did : o.getDepts()) {
                MpsDeptUser mpsDeptUser = new MpsDeptUser();
                mpsDeptUser.setId(UUID.randomUUID().toString().toUpperCase().trim());
                mpsDeptUser.setUserId(o.getId());
                mpsDeptUser.setDeptId(did);
                mpsDeptUser.setModifyOwner(Owner);
                mpsDeptUser.setModifyTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                mpsDeptUserDao.save(mpsDeptUser);
            }
        }
    }

    @Override
    public MpsUser findMpsUserById(String id) {
        return dao.findMpsUserById(id);
    }

    @Override
    public MpsUser findMpsUserByName(String name) {
        return dao.findMpsUserByName(name);
    }

    @Override
    public int count(Map<String, Object> map) {
        return dao.count(map);
    }

    @Override
    public List<MpsUser> findMpsUsers(Map<String, Object> map) {
        return dao.findMpsUsers(map);
    }

}
